<?php

// +----------------------------------------------------------------------
// |供应商服务层
// +----------------------------------------------------------------------

namespace app\common\system;

use think\facade\Db;

/**
 * 店铺
 */
class ShopsService
{ 
    /**
    * 获取的店铺分组的数据
    * @param array $param 前台传过来的参数  
    * @return array
    */
   public static function   getlist($param, $categoinfo){
      
       $params= $param['params'];  
       $field='id,name,platform,status,shop_author,createtime,category_id,iswatermark';    
        $where = [];    
        if($params['name']){
            $where[] = ['name', 'like', '%'. $params['name']. '%'];
        }
        // if($params['platform']){
        //     $where[] = ['platform', '=',$params['platform']];
        // }s
        if($params['status']==2){
            $where[] = ['status', '=', 0];
        }else  if($params['status']==1){
            $where[] = ['status', '=', 1];
        } 
        //是否添加水印
        if($params['iswatermark']==2){
            $where[] = ['iswatermark', '=', 0];
        }else  if($params['iswatermark']==1){
            $where[] = ['iswatermark', '=', 1];
        } 
        $shops_list = Db::name('shops')->field($field)->where($where)->order('id asc,status desc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();   
        $categoinfo=array_column($categoinfo,null,'catepubid');       
       
        if($shops_list['data']){
             foreach ($shops_list['data'] as $key => $value) {
                //  $Supplier_list['data'][$key]['sp_author'] =json_decode($value['sp_author'],true);
                 $shop_author=json_decode($value['shop_author'],true);
                 $category_id=explode(',',$value['category_id']);
                 $shops_list['data'][$key]['category_id']=$category_id;

                 $names = array_map(function($id) use ($categoinfo) {
                    return $categoinfo[$id]['pubnameCn'] ?? "未知";
                }, $category_id);

                // 4. 拼接成最终字符串
                $shops_list['data'][$key]['category_name']=implode(",", $names);
                if($shop_author){           
                    $shops_list['data'][$key]['username']=$shop_author['username'];
                    $shops_list['data'][$key]['tel']=$shop_author['tel'];
                    $shopshops_lists_list['data'][$key]['address']=$shop_author['address'];
                }
             }
        }
        $data['list'] = $shops_list['data'];
        $data['total'] = $shops_list['total'];    
        return $data; 
   }

      /**
     * 修改店铺
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function create($param){
        $params= $param['params'];  
       
        $category_id=implode(',',$params['category_id']); //经营的品类
        $shop_author=['username'=>isset($params['username'])?$params['username']:'','tel'=>isset($params['tel'])?$params['tel']:'','address'=>isset($params['address'])?$params['address']:''];
        $data=[
            'iswatermark'=> $params['iswatermark'],
            'category_id'=> $category_id,
            'platform'=> $params['platform'],
            'status'=> $params['status'],
            'name' => $params['name'],
            'shop_author' =>json_encode($shop_author,JSON_UNESCAPED_UNICODE),         
        ];

        if($params['id']){
               //有值则说明是修改 
               $result =  Db::name('shops')->where('id',$params['id'])->update($data);    
        }else{
            //执行添加数据
             $sp_author=['appSecret'=>$params['appSecret'],'appKey'=>$params['appKey']];//店铺授权信息
            $data['createtime']=datetime();
            $data['createymd']=(int)date('Ymd',time());
            $data['sp_author']=json_encode( $sp_author,JSON_UNESCAPED_UNICODE);
            $result =  Db::name('shops')->save($data);
        }
        if($result){
            return true;
        }else{
            error('修改失败');
        }       
    }

    public static function update_status($param){
        $params= $param['params'];  
        $data=[
           'status'=> $params['status'],
        ];
        $result =  Db::name('shops')->where('id',$params['id'])->update($data);
        if($result){
            return true;
        }else{
            error('修改失败');
        }
    }

}